#include "OptionsMenuState.h"
#include "MainMenuState.h"
#include "CSGD_DirectInput.h"
#include "CSGD_TextureManager.h"
#include "CSGD_Direct3D.h"
#include "CSGD_XAudio2.h"
#include "BitmapFont.h"
#include "../TinyXML/tinyxml.h"
#include <iostream>
#include "Game.h"
#include <sstream>
#include "../resource.h"
using namespace std;



/////////////////////////////////////////////////////////////////////////////
// Function		: COptionsMenuState 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
COptionsMenuState::COptionsMenuState(void)
{
	m_nCursor = 0;	
	m_nBackgroundImg = -1;
	m_nMusicVolume = 100;
	m_nSfxVolume = 100;
	m_pPrevState = nullptr;
	English = true;
}

/////////////////////////////////////////////////////////////////////////////
// Function		: ~COptionsMenuState 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
COptionsMenuState::~COptionsMenuState(void)
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: GetInstance 
// Return		: COptionsMenuState* 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
COptionsMenuState* COptionsMenuState::GetInstance( void )
{
	static COptionsMenuState s_Instance;

	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void COptionsMenuState::Enter( void )
{
	m_nBackgroundImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MenuBackground.png"));


	if(English)
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_MusVol, MusVol, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_SFXVol, SFXVol, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_EscToMain, EscToMain, 50);
		
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_FullOpT, FullScreenT, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_FullOpF, FullScreenF, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_LangT, LangSelectE, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_LangF, LangSelectS, 30);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_MusVol, MusVol, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_SFXVol, SFXVol, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_EscToMain, EscToMain, 50);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_FullOpT, FullScreenT, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_FullOpF, FullScreenF, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_LangT, LangSelectE, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_LangF, LangSelectS, 30);
	}

	TrueEscTMain = "";

	for (int character = 0; character < 50; character++)
	{
		if(EscToMain[character] != NULL)
			TrueEscTMain += (char)EscToMain[character];
		else
			break;
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Exit 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void COptionsMenuState::Exit ( void )
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImg);

}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input 
// Return		: bool
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool COptionsMenuState::Input( void )
{
	CSGD_DirectInput* m_pDI = CSGD_DirectInput::GetInstance();

	if( m_pDI->KeyPressed( DIK_ESCAPE ) == true || m_pDI->JoystickButtonPressed(1,0))
	{
		if(m_nCursor == 1)
			CSGD_XAudio2::GetInstance()->MusicPlaySong(CGame::GetInstance()->GetMusicID(), true);


		CGame::GetInstance()->ChangeState(m_pPrevState);
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
	}

	else if( m_pDI->KeyPressed( DIK_UP ) == true || m_pDI->JoystickDPadPressed( DIR_UP ) == true)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		if( m_nCursor > 0 )	
			m_nCursor--;

		if(m_nCursor == 0)
			CSGD_XAudio2::GetInstance()->MusicPlaySong(CGame::GetInstance()->GetMusicID(), true);

	}
	else if( m_pDI->KeyPressed( DIK_DOWN ) == true || m_pDI->JoystickDPadPressed( DIR_DOWN ) == true)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		if( m_nCursor < 3 )	
			m_nCursor++;

		if(m_nCursor == 1)
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetSfxID());
	
	}

	else if (m_pDI->KeyPressed( DIK_LEFT ) == true|| m_pDI->JoystickDPadPressed(DIR_LEFT))
	{
		if (m_nCursor == 0 && m_nMusicVolume > 0) 
		{
			m_nMusicVolume = m_nMusicVolume - 5;

			CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(((float)m_nMusicVolume)/100.0f);

		}

		else if (m_nCursor == 1 && m_nSfxVolume > 0)
		{
			m_nSfxVolume = m_nSfxVolume - 5;
			CSGD_XAudio2::GetInstance()->SFXSetMasterVolume( ((float)m_nSfxVolume)/100.0f);
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetSfxID());
		}

		else if ( m_nCursor == 2)
		{
			CGame::GetInstance()->ToggleFullScreen();
		}
		else if (m_nCursor == 3)
			English = !English;

	}

	else if (m_pDI->KeyPressed( DIK_RIGHT ) == true || m_pDI->JoystickDPadPressed(DIR_RIGHT))
	{
		if (m_nCursor == 0 && m_nMusicVolume < 100)
		{
			m_nMusicVolume = m_nMusicVolume + 5;
			CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(((float)m_nMusicVolume)/100.0f);
			//CSGD_XAudio2::GetInstance()->MusicPlaySong(CGame::GetInstance()->GetMusicID());
		}

		else if (m_nCursor == 1 && m_nSfxVolume < 100)
		{
			m_nSfxVolume = m_nSfxVolume +5;
			CSGD_XAudio2::GetInstance()->SFXSetMasterVolume( ((float)m_nSfxVolume)/100.0f);
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetSfxID());
		}

		else if ( m_nCursor == 2)
		{
			CGame::GetInstance()->ToggleFullScreen();
		}
		else if( m_nCursor == 3)
			English = !English;

	}

	return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update 
// Parameters	: fElapsedTime - Ammount of time passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void COptionsMenuState::Update( float fElapsedTime )
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void COptionsMenuState::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImg, 0,0, 1.0f,1.0f);

	CBitmapFont* font = CGame::GetInstance()->GetFont();

	wostringstream Musicout, Sfxout;;

	Musicout  << MusVol << (int)(m_nMusicVolume);
	Sfxout <<  SFXVol << (int)(m_nSfxVolume);

	font->Draw( Musicout.str().c_str(), 100, 100, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw(Sfxout.str().c_str(), 100, 200, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	if(!CGame::GetInstance()->GetFullScreen())
	{
		font->Draw(FullScreenT, 100, 300, 1.0f, 
			D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}
	else
	{
		font->Draw(FullScreenF, 100, 300, 1.0f, 
			D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}

	if(English)
	{
		font->Draw(LangSelectE, 100, 400, 1.0f, 
			D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}
	else
	{
		font->Draw(LangSelectS, 100, 400, 1.0f, 
			D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}

	font->Draw( EscToMain, CGame::GetInstance()->GetScreenMidPoint(TrueEscTMain, 1.0f), 
		568,1.0f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw( _T(">"), 50, 100 + (100 * m_nCursor), 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
}

void COptionsMenuState::LoadOptionsXML(string szfilepath)
{
		CSGD_XAudio2 * m_pXA = CSGD_XAudio2::GetInstance();
		TiXmlDocument doc;

		if(doc.LoadFile(szfilepath.c_str()) == false)
			return;

		int vol;
		int fscreen;

		TiXmlElement* pRoot = doc.RootElement();

	TiXmlElement* pMusic = pRoot->FirstChildElement("Music");
	pMusic->Attribute("Volume",&vol);
	m_nMusicVolume = vol;
		m_pXA->MusicSetMasterVolume(((float)vol)/100.0f);

		TiXmlElement* pSound = pRoot->FirstChildElement("Sound");
		pSound->Attribute("Volume",&vol);
		m_nSfxVolume = vol;
		CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(((float)vol)/100.0f);
		


		TiXmlElement* pFscreen = pRoot->FirstChildElement("FullScreen");
		pFscreen->Attribute("Bool", &fscreen);

		if(fscreen == 0)
		{
			CGame::GetInstance()->SetFullScreen(false);
			CSGD_Direct3D::GetInstance()->Resize(CGame::GetInstance()->GetScreenWidth(),
				CGame::GetInstance()->GetScreenHeight(), false);
		}
		else
		{
			CGame::GetInstance()->SetFullScreen(true);
			CSGD_Direct3D::GetInstance()->Resize(CGame::GetInstance()->GetScreenWidth(),
				CGame::GetInstance()->GetScreenHeight(), true);
		}
	int Language = 0;
	TiXmlElement* pEnglish = pRoot->FirstChildElement("English");
	pEnglish->Attribute("Bool",&Language);

	if(Language == 1)
		English = true;
	else
		English = false;


}

void COptionsMenuState::SaveOptionsXML(string szfilepath)
{
TiXmlDocument doc;

	TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "utf-8", "");
	doc.LinkEndChild(pDecl);

	TiXmlElement* pRoot = new TiXmlElement("Options");
	doc.LinkEndChild(pRoot);

	TiXmlElement* pMusicOption = new TiXmlElement("Music");
	pMusicOption->SetAttribute("Volume", m_nMusicVolume);
	pRoot->LinkEndChild(pMusicOption);

	TiXmlElement* pSoundOption = new TiXmlElement("Sound");
	pSoundOption->SetAttribute("Volume", m_nSfxVolume);
	pRoot->LinkEndChild(pSoundOption);

	TiXmlElement* pFullscreenOption = new TiXmlElement("FullScreen");
	pFullscreenOption->SetAttribute("Bool", (int)(CGame::GetInstance()->GetFullScreen()));
	pRoot->LinkEndChild(pFullscreenOption);

	int Language = English;
	TiXmlElement* pEnglishChoice = new TiXmlElement("English");
	pEnglishChoice->SetAttribute("Bool", Language);
	pRoot->LinkEndChild(pEnglishChoice);

	doc.SaveFile("Options.xml");

}
